package com.fpd.tools;

import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * @desc 根据出生日期计算年龄(精确到天)
 * @author wujiangbo
 * @date 2019年12月30日 下午4:52:15
 */
public class AgeUtil {
	public static void main(String[] args) {
		System.out.println(getAgeDetail("1991-05-13"));
	}

	public static String getAgeDetail(String date) {
		// 如果有空格
		int index = date.indexOf(" ");
		if (index != -1) {
			date = date.substring(0, index);
		}
		String[] data = date.split("-");
		Calendar birthday = new GregorianCalendar(Integer.valueOf(data[0]), Integer.valueOf(data[1]), Integer.valueOf(data[2]));
		Calendar now = Calendar.getInstance();
		int day = now.get(Calendar.DAY_OF_MONTH) - birthday.get(Calendar.DAY_OF_MONTH);
		// 月份从0开始计算，所以需要+1
		int month = now.get(Calendar.MONTH) + 1 - birthday.get(Calendar.MONTH);
		int year = now.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
		// 按照减法原理，先day相减，不够向month借；然后month相减，不够向year借；最后year相减。
		if (day < 0) {
			month -= 1;
			now.add(Calendar.MONTH, -1);// 得到上一个月，用来得到上个月的天数。
			day = day + now.getActualMaximum(Calendar.DAY_OF_MONTH);
		}
		if (month < 0) {
			month = (month + 12) % 12;
			year--;
		}
		StringBuffer tag = new StringBuffer();
		if (year > 0) {
			tag.append(year + "岁");
		}
		if (month > 0) {
			tag.append(month + "个月");
		}
		if (day > 0) {
			tag.append(day + "天");
		}
		if (year == 0 && month == 0 && day == 0) {

			tag.append("今日出生");
		}
		return String.valueOf(tag);
	}

	public static int getAgePart(int age) {
		int agePart = 0;
		if (age < 25) {
			agePart = 1;
		} else if (25 <= age && age <= 29) {
			agePart = 2;
		} else if (29 < age && age <= 34) {
			agePart = 3;
		} else if (34 < age && age <= 39) {
			agePart = 4;
		} else if (39 < age && age <= 44) {
			agePart = 5;
		} else if (45 <= age) {
			agePart = 6;
		}
		return agePart;
	}
	
	public static String getByAge(int age) {
		if (age < 25) {
			return "小于25";
		} else if (25 <= age && age <= 29) {
			return "25-29";
		} else if (29 < age && age <= 34) {
			return "29-34";
		} else if (34 < age && age <= 39) {
			return "35-39";
		} else if (39 < age && age <= 44) {
			return "40-44";
		} else if (45 <= age) {
			return "大于44";
		}
		return "";
	}
	
	public static String getByPart(int part) {
		if (part == 1) {
			return "小于25";
		} else if (part == 2) {
			return "25-29";
		} else if (part == 3) {
			return "29-34";
		} else if (part == 4) {
			return "35-39";
		} else if (part == 5) {
			return "40-44";
		} else if (part == 6) {
			return "大于44";
		}
		return "";
	}

	public static int getAging(int day) {
		int aging = 0;
		if (day == 0) {
			aging = 0;
		} else if (day < 365) {
			aging = 1;
		} else if (365 <= day && day < 730) {
			aging = 2;
		} else if (730 <= day && day < 1095) {
			aging = 3;
		} else if (day >= 1095) {
			aging = 4;
		}
		return aging;
	}

	public static int getVisitBackCycle(int visitBackDay) {
		int visitBackCycle = 0;
		if (visitBackDay == 0){
			visitBackCycle = 0;
		}else if (1 <= visitBackDay && visitBackDay <= 5){
			visitBackCycle = 1;
		}else if (6 <= visitBackDay && visitBackDay <= 10){
			visitBackCycle = 2;
		}else if (11 <= visitBackDay && visitBackDay <= 15){
			visitBackCycle = 3;
		}else if (16 <= visitBackDay && visitBackDay <= 20){
			visitBackCycle = 4;
		}else if (21 <= visitBackDay && visitBackDay <= 25){
			visitBackCycle = 5;
		}else if (26 <= visitBackDay && visitBackDay <= 30){
			visitBackCycle = 6;
		}else if (31 <= visitBackDay && visitBackDay <= 40){
			visitBackCycle = 7;
		}else if (41 <= visitBackDay && visitBackDay <= 50){
			visitBackCycle = 8;
		}else if (50 <= visitBackDay){
			visitBackCycle = 9;
		}
		return visitBackCycle;
	}

}
